package com.huawei.appmarket.framework.widget;

import android.content.Context;
import android.database.ContentObserver;
import android.os.Handler;
import android.provider.Settings.Global;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;
import android.widget.OverScroller;
import androidx.viewpager.widget.ViewPager;
import huawei.widget.HwSubTabWidget;
import java.lang.ref.WeakReference;
import o.Logger;
import o.ContentManager;
import o.epv;

public class ExpandScrollLayout extends LinearLayout {
    private static final int ˊ = epv.ˋ(ContentManager.getInstant().getContext(), 48);
    private float ʻ;
    private boolean ʻॱ;
    private VelocityTracker ʼ;
    private OverScroller ʽ;
    private boolean ˊॱ = false;
    protected int ˋ;
    private ViewPager ˋॱ;
    protected int ˎ;
    protected LinearLayout ˏ;
    private boolean ˏॱ;
    private boolean ͺ;
    float ॱ;
    private HwSubTabWidget ॱˊ;
    private boolean ॱˋ;
    private boolean ॱˎ = false;
    private int ॱॱ;
    private b ॱᐝ = new b(this, new Handler());
    private float ᐝ;
    private c ᐝॱ;

    static class b extends ContentObserver {
        private WeakReference<ExpandScrollLayout> ˊ;

        public b(ExpandScrollLayout expandScrollLayout, Handler handler) {
            super(handler);
            this.ˊ = new WeakReference(expandScrollLayout);
        }

        public void onChange(boolean z) {
            if (this.ˊ != null) {
                ExpandScrollLayout expandScrollLayout = (ExpandScrollLayout) this.ˊ.get();
                if (expandScrollLayout != null && expandScrollLayout.ˏॱ) {
                    expandScrollLayout.requestLayout();
                    expandScrollLayout.invalidate();
                }
            }
        }
    }

    public interface c {
        boolean ˏ();
    }

    public ExpandScrollLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        setOrientation(1);
        this.ʽ = new OverScroller(context);
        this.ॱॱ = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public void setHeadView(LinearLayout linearLayout) {
        this.ˏ = linearLayout;
    }

    public void setViewPager(ViewPager viewPager) {
        this.ˋॱ = viewPager;
    }

    public void setScrollableTab(HwSubTabWidget hwSubTabWidget) {
        this.ॱˊ = hwSubTabWidget;
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (this.ˏॱ) {
            ˊ();
        }
    }

    private void ˋ() {
        try {
            getContext().getContentResolver().registerContentObserver(Global.getUriFor("navigationbar_is_min"), false, this.ॱᐝ);
        } catch (Exception e) {
            Logger.e("ExpandScrollLayout", "registerNavigationBarObserver, ex: " + e.toString());
        }
    }

    private void ˊ() {
        try {
            getContext().getContentResolver().unregisterContentObserver(this.ॱᐝ);
        } catch (Exception e) {
            Logger.e("ExpandScrollLayout", "unRegisterNavigationBarObserver, ex: " + e.toString());
        }
    }

    public boolean ˎ() {
        return this.ˏॱ;
    }

    public void ˋ(boolean z) {
        this.ॱˋ = z;
        this.ʻॱ = !z;
    }

    public void setHasExpandLayout(boolean z) {
        this.ˏॱ = z;
        if (z) {
            this.ˋ = ˊ;
            this.ˎ = ˊ;
            ˋ();
        }
    }

    private void ॱ() {
        if (this.ʼ == null) {
            this.ʼ = VelocityTracker.obtain();
        }
    }

    private void ˏ() {
        if (this.ʼ != null) {
            this.ʼ.recycle();
            this.ʼ = null;
        }
    }

    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        if (this.ˏॱ) {
            int action = motionEvent.getAction();
            float y = motionEvent.getY();
            switch (action) {
                case 0:
                    this.ᐝ = y;
                    this.ॱ = y;
                    break;
                case 2:
                    return ˋ(motionEvent, y);
            }
            this.ॱ = y;
        }
        return super.dispatchTouchEvent(motionEvent);
    }

    private boolean ˋ(MotionEvent motionEvent, float f) {
        float f2 = f - this.ॱ;
        if (Math.abs(f - this.ᐝ) <= ((float) this.ॱॱ) || this.ˊॱ || ((!ʻ() || !this.ʻॱ || f2 <= 0.0f) && ((this.ʻॱ || this.ॱˋ) && ((!this.ॱˋ || !ʻ()) && (!this.ॱˋ || f2 >= 0.0f))))) {
            return super.dispatchTouchEvent(motionEvent);
        }
        this.ˊॱ = true;
        motionEvent.setAction(3);
        MotionEvent obtain = MotionEvent.obtain(motionEvent);
        dispatchTouchEvent(motionEvent);
        obtain.setAction(0);
        this.ॱ = f;
        return dispatchTouchEvent(obtain);
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        boolean z = false;
        if (this.ˏॱ) {
            ॱ();
            this.ʼ.addMovement(motionEvent);
            int action = motionEvent.getAction();
            float y = motionEvent.getY();
            switch (action) {
                case 0:
                    if (!this.ʽ.isFinished()) {
                        this.ʽ.abortAnimation();
                    }
                    this.ᐝ = y;
                    return true;
                case 1:
                    this.ͺ = false;
                    break;
                case 2:
                    float f = y - this.ᐝ;
                    float f2 = y - this.ʻ;
                    if ((f2 <= 0.0f || f <= 0.0f) && (f2 >= 0.0f || f >= 0.0f)) {
                        z = true;
                    }
                    ˊ(f, motionEvent);
                    if (z) {
                        this.ᐝ = y;
                    }
                    this.ʻ = y;
                    break;
                case 3:
                    this.ͺ = false;
                    ˏ();
                    if (!this.ʽ.isFinished()) {
                        this.ʽ.abortAnimation();
                        break;
                    }
                    break;
            }
        }
        return super.onTouchEvent(motionEvent);
    }

    private void ˊ(float f, MotionEvent motionEvent) {
        if (!this.ͺ && Math.abs(f) > ((float) this.ॱॱ)) {
            this.ͺ = true;
        }
        if (this.ͺ) {
            if (this.ʽ.isFinished()) {
                if (f > ((float) ˊ)) {
                    ॱॱ();
                } else if (f < ((float) (-ˊ))) {
                    ᐝ();
                }
            }
            if (ˊ(f)) {
                motionEvent.setAction(0);
                dispatchTouchEvent(motionEvent);
                this.ˊॱ = false;
                this.ॱˎ = true;
                postDelayed(new Runnable(this) {
                    final /* synthetic */ ExpandScrollLayout ˋ;

                    {
                        this.ˋ = r1;
                    }

                    public void run() {
                        this.ˋ.ॱˎ = false;
                    }
                }, 50);
            }
        }
    }

    private boolean ˊ(float f) {
        if ((getScrollY() != this.ˎ || f >= 0.0f) && (getScrollY() != 0 || f <= 0.0f || ʻ())) {
            return false;
        }
        return true;
    }

    private void ˏ(int i, int i2) {
        int scrollY = getScrollY();
        this.ʽ.startScroll(0, scrollY, 0, i - scrollY, i2);
        invalidate();
    }

    private void ᐝ() {
        ˏ(this.ˎ, 300);
    }

    private void ॱॱ() {
        ˏ(0, 300);
    }

    public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
        if (this.ˏॱ) {
            int action = motionEvent.getAction();
            float y = motionEvent.getY();
            if (ˋ(action)) {
                switch (action) {
                    case 0:
                        this.ᐝ = y;
                        break;
                    case 2:
                        float f = y - this.ᐝ;
                        if (Math.abs(f) > ((float) this.ॱॱ)) {
                            this.ͺ = true;
                            if (this.ॱˋ && !ʻ() && f > 0.0f && this.ʽ.isFinished()) {
                                this.ˊॱ = false;
                                return false;
                            } else if (!this.ʻॱ) {
                                return true;
                            } else {
                                if (ʻ() && this.ʻॱ && f > 0.0f) {
                                    return true;
                                }
                            }
                        }
                        break;
                }
            }
            return true;
        }
        return super.onInterceptTouchEvent(motionEvent);
    }

    private boolean ˋ(int i) {
        if (!this.ˊॱ && this.ॱˎ && i == 1 && ʻ() && this.ʻॱ) {
            return false;
        }
        if (this.ʽ.isFinished() || !this.ˊॱ) {
            return true;
        }
        return false;
    }

    public void setOnScrollListener(c cVar) {
        this.ᐝॱ = cVar;
    }

    private boolean ʻ() {
        if (this.ᐝॱ != null) {
            return this.ᐝॱ.ˏ();
        }
        return false;
    }

    public void scrollTo(int i, int i2) {
        boolean z;
        boolean z2 = true;
        this.ʻॱ = false;
        if (i2 < 0) {
            this.ॱˋ = true;
            i2 = 0;
        }
        if (i2 >= this.ˎ) {
            i2 = this.ˎ;
            this.ʻॱ = true;
            this.ॱˋ = false;
        }
        if (i2 != getScrollY()) {
            super.scrollTo(i, i2);
        }
        if (getScrollY() == 0) {
            z = true;
        } else {
            z = false;
        }
        this.ॱˋ = z;
        if (getScrollY() != this.ˎ) {
            z2 = false;
        }
        this.ʻॱ = z2;
    }

    public void computeScroll() {
        if (this.ʽ.computeScrollOffset()) {
            scrollTo(0, this.ʽ.getCurrY());
            invalidate();
        }
    }

    protected void ˎ(int i, int i2) {
        if (ˎ() && this.ˏ != null && this.ॱˊ != null && this.ˋॱ != null) {
            this.ˏ.measure(i, MeasureSpec.makeMeasureSpec(0, 0));
            this.ॱˊ.measure(i, MeasureSpec.makeMeasureSpec(0, 0));
            this.ˋ = this.ˏ.getMeasuredHeight();
            this.ˎ = this.ˋ;
            this.ˋॱ.getLayoutParams().height = getMeasuredHeight() - this.ॱˊ.getMeasuredHeight();
            setMeasuredDimension(getMeasuredWidth(), (this.ˋ + this.ॱˊ.getMeasuredHeight()) + this.ˋॱ.getMeasuredHeight());
            if (Logger.ˎ()) {
                Logger.ˎ("ExpandScrollLayout", "onMeasure, getMeasuredHeight = " + getMeasuredHeight() + ", viewPager.height = " + this.ˋॱ.getMeasuredHeight() + ", scrollableTab.height = " + this.ॱˊ.getMeasuredHeight());
            }
        }
    }

    protected void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        ˎ(i, i2);
    }
}
